import org.hibernate.Session;
import org.hibernate.SessionFactory;
import org.hibernate.Transaction;
import org.hibernate.cfg.Configuration;
import org.hibernate.query.NativeQuery;
import org.hibernate.query.Query;
import pojo.Cat;
import pojo.Name;
import pojo.SexType;
import pojo.User;

import javax.persistence.criteria.CriteriaBuilder;
import javax.persistence.criteria.CriteriaQuery;
import javax.persistence.criteria.Path;
import javax.persistence.criteria.Root;
import java.util.List;

public class Test {
    public static void main(String[] args) {
        Configuration configuration = new Configuration().configure();
        SessionFactory sessionFactory = configuration.buildSessionFactory();
        Session session = sessionFactory.openSession();
        Transaction transaction = session.beginTransaction();

//        extracted(session);


//        hql查询
//        select(session);

//        更新操作
//        update(session);

//        criteria查询
        criteria(session);

//        sql查询
        String sql = "select * from cat";
        NativeQuery sqlQuery = session.createSQLQuery(sql);
        sqlQuery.addEntity(Cat.class);
        List<Cat> cats = sqlQuery.list();
        for (Cat cat: cats){
            System.out.println(cat);
        }


        transaction.commit();
        session.close();
        sessionFactory.close();
    }

    private static void criteria(Session session) {
        //        查询所有
        CriteriaBuilder criteriaBuilder = session.getCriteriaBuilder();
        CriteriaQuery<Cat> criteria = criteriaBuilder.createQuery(Cat.class);
        Root<Cat> root = criteria.from(Cat.class);
        criteria.select(root);
        List<Cat> list = session.createQuery(criteria).list();
        for (Cat c: list){
            System.out.println(c);
        }

//        查询单一属性
        CriteriaQuery<Double> query = criteriaBuilder.createQuery(Double.class);
        Root<Cat> root1 = query.from(Cat.class);
        Path<Double> price = root1.get("price");
        query.select(price);
        query.where(criteriaBuilder.equal(root1.get("name"),"cat2"));
        List<Double> list1 = session.createQuery(query).list();
        for (Double d: list1){
            System.out.println(d);
        }

//        查询多个属性
        CriteriaQuery<Object[]> query1 = criteriaBuilder.createQuery(Object[].class);
        Root<Cat> root2 = query1.from(Cat.class);
        Path<String> path1 = root2.get("name");
        Path<Double> path2 = root2.get("price");
        query1.select(criteriaBuilder.array(path1,path2));
        List<Object[]> list2 = session.createQuery(query1).list();
        for (Object[] obj: list2){
            System.out.println(obj[0] + "       " +obj[1]);
        }
    }

    private static void update(Session session) {
        String update = "update cat c set c.price = : price where c.name = : name";
        Query query = session.createQuery(update)
                .setParameter("price",2000d)
                .setParameter("name","cat1");
        if(query.executeUpdate() == 1){
            System.out.println("修改成功");
        }else
            System.out.println("修改失败");
    }

    private static void select(Session session) {
        Query<Cat> query = session.createQuery("from cat", Cat.class).setTimeout(2);
        List<Cat> result = query.list();
        for (Cat cat: result) {
            System.out.println(cat);
        }

        //查询单一数据
        String hql = "select c.name from cat c where c.price >= : price";
        Query<String> query1 = session.createQuery(hql, String.class).setTimeout(2)
                .setParameter("price",600d);
        List<String> result1 = query1.list();
        for (String s: result1) {
            System.out.println(s);
        }

        //查询多个数据
        String hql1 = "select c.name,c.price from cat c where c.price >= : price";
        Query<Object[]> query2 = session.createQuery(hql1, Object[].class).setTimeout(2)
                .setParameter("price",600d);
        List<Object[]> result2 = query2.list();
        for (Object[] obj: result2) {
            System.out.println(obj[0] + "       " + obj[1]);
        }
    }

    private static void extracted(Session session) {
        for (int i = 0; i < 5; i++) {
            Cat cat = new Cat();
            cat.setName("cat" + i);
            cat.setPrice(300 * i);
            session.save(cat);
        }
    }
}
